import { _decorator, Component, Node, Camera, Vec3, tween, math} from 'cc';

const { ccclass, property } = _decorator;

@ccclass('DirectorManager') // 用 @ccclass 声明类为 Cocos 组件
export class DirectorManager extends Component {
    // 通过 @property 让编辑器可以配置属性
    @property(Node) mainCamera!: Node;
    @property(Node) touchCamera!: Node;
    @property(Node) lights!: Node;
    @property(Node) playMode!: Node;

    @property([Node]) touchGroups: Node[] = [];

	static instance: DirectorManager;

    private angle = 0;
    private baseTouchPos = new Vec3(0, 0, 0);

    onLoad() {
        DirectorManager.instance = this;
        this.baseTouchPos = new Vec3(this.touchCamera.position);
    }

    changeViewPort(index:number) {
        switch(index) {
            case 0:
                break;
            case 1:
                this.angle = 90;
                this.rotateCamera(90, 1);
                break;
            case 2:
                this.angle = -180;
                this.rotateCamera(-180, 2);
                break;
            case 3:
                this.angle = -90;
                this.rotateCamera(-90, 1);
                break;
            default:
        }
    }

    rotateCamera(angleTarget:number, deltaTime:number) {
        // const camera = this.mainCamera;
        // const radius: number = 0.91; // 水平半径
        // const height: number = 0.893; // 高度
        // const duration = deltaTime; // 动画持续时间（秒）

        // const lights = this.lights;

        // // 创建 tween 动画
        // tween(camera)
        // .to(
        //     duration,
        //     {},
        //     {
        //         onUpdate: (target:any, ratio:any) => {
        //             const angle = math.lerp(0, angleTarget, ratio); // 当前角度（从 0 到 360 度）
        //             const radian = math.toRadian(angle); // 转为弧度
        //             // 计算新位置
        //             const x = radius * Math.sin(radian);
        //             const z = radius * Math.cos(radian);
        //             const newPos = new Vec3(x, height, z);

        //             // 更新摄像机位置
        //             camera.setPosition(newPos);

        //             camera.eulerAngles = new Vec3(-46, angle, 0);
        //             lights.eulerAngles = new Vec3(0, angle, 0);
        //         },
        //     }
        // )
        // .start();

        this.rotateObject(this.mainCamera, angleTarget, deltaTime);
        this.rotateObject(this.touchCamera, angleTarget, deltaTime);
        this.rotateObject(this.lights, angleTarget, deltaTime);
        this.rotateObject(this.playMode, angleTarget, deltaTime);

    }


    rotateObject(object:Node, angleTarget:number, deltaTime:number) {
        const pos = object.position;
        const euler = object.eulerAngles;
        const radius: number = pos.z; // 水平半径
        const height: number = pos.y; // 高度
        const duration = deltaTime; // 动画持续时间（秒）
        // 创建 tween 动画
        tween(object)
        .to(
            duration,
            {},
            {
                onUpdate: (target:any, ratio:any) => {
                    const angle = math.lerp(0, angleTarget, ratio); // 当前角度（从 0 到 360 度）
                    const radian = math.toRadian(angle); // 转为弧度
                    // 计算新位置
                    const x = radius * Math.sin(radian);
                    const z = radius * Math.cos(radian);
                    const newPos = new Vec3(x, height, z);
                    // 更新位置 角度
                    object.setPosition(newPos);
                    object.eulerAngles = new Vec3(euler.x, angle, euler.z);
                },
            }
        )
        .start();
    }

    recordFirstViewport(){
        this.baseTouchPos = this.touchCamera.position;
    }

    setFirstViewport(level:number) {
        // this.touchCamera.position
        console.log("setFirstViewport...", level);
        if(level < 5)
        {
            for(let i = 0; i < 5; i++)
            {
                if(i >= level)
                {
                    this.touchGroups[i].active = true;
                }
                else
                {
                    this.touchGroups[i].active = false;
                }
            }

            if(this.touchCamera)
            {

                const radian = math.toRadian(this.angle);
                const x = this.baseTouchPos.z * Math.sin(radian);
                const z = this.baseTouchPos.z * Math.cos(radian);
                const basePos = new Vec3(x, this.baseTouchPos.y, z);

                if(level > 1)
                {
                    const offsetRate = level - 1;
                    const angelOffset = this.angle + 90;
                    const radianOffset = math.toRadian(angelOffset);
                    const ox = 0.0305 * offsetRate * Math.sin(radianOffset);
                    const oz = 0.0305 * offsetRate * Math.cos(radianOffset);

                    basePos.x += ox;
                    basePos.z += oz;
                }

                this.touchCamera.position = basePos;
            }
        }
        else
        {
            for(let i = 0; i < 5; i++)
            {
                this.touchGroups[i].active = false;
            }
        }

    }

}